package com.example.demo.ai.llm.service;

import com.jfinal.kit.Kv;
import java.util.List;
import java.util.regex.Pattern;
import org.dromara.hutool.core.regex.ReUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.noear.solon.ai.chat.ChatResponse;
import org.noear.solon.ai.chat.message.ChatMessage;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.flow.ChainContext;
import org.noear.solon.flow.Node;
import org.noear.solon.flow.TaskComponent;

/**
 * @author airhead
 */
@Component("searchTask")
public class SearchTask implements TaskComponent {
  @Inject RepositoryService repositoryService;

  @Override
  public void run(ChainContext context, Node node) throws Throwable {

    Kv model = Kv.create().set(context.model());
    String prompt = model.getStr("prompt");

    // 获取上下文
    List<String> ctx = repositoryService.search(prompt);
    context.put("ctx", String.join("\n\n", ctx));

    // 获取病史
    String history = repositoryService.history();
    context.put("history", history);
  }
}
